<template>
  <div class="login-container">
    <div class="bg"></div>
    <div class="loginBox">
      <!-- <img class="cn" src="~@/assets/images/cn.png" />
      <img class="en" src="~@/assets/images/en.png" /> -->

      <div class="loginForm">
        <img class="left decr" src="~@/assets/images/login/left.png" />
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          label-position="left"
        >
          <div class="loginMain">
            <div class="title-container">
              <h3 class="title"> 欢迎登陆管理平台</h3>
            </div>

            <el-form-item prop="username">
              <span class="svg-container">
                <svg-icon icon-class="user" />
              </span>
              <!-- autocomplete="on" -->

              <el-input
                ref="username"
                size="small"
                v-model="loginForm.username"
                placeholder="请输入用户名"
                name="username"
                type="text"
                tabindex="1"
              />
            </el-form-item>

            <!-- <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual> -->
            <el-form-item prop="password">
              <span class="svg-container">
                <svg-icon icon-class="password" />
              </span>
              <!-- autocomplete="on" -->

              <el-input
                :key="passwordType"
                ref="password"
                v-model="loginForm.password"
                :type="passwordType"
                placeholder="请输入密码"
                name="password"
                tabindex="2"
                
                @blur="capsTooltip = false"
                @keyup.enter.native="handleLogin"
              />
              <span class="show-pwd" @click="showPwd">
                <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
              </span>
            </el-form-item>
            <!-- </el-tooltip> -->
<!-- @keyup.native="checkCapslock" -->
            <el-form-item prop="code">
              <span class="svg-container">
                <svg-icon icon-class="user" />
              </span>
              <!-- autocomplete="on" -->

              <el-input
                ref="code"
                size="small"
                v-model="loginForm.code"
                placeholder="请输入验证码"
                name="code"
                type="text"
                tabindex="3"
              />
              <!-- <span class="show-pwd getCode" @click="getCode" :style="imageCode">获取验证码</span> -->
              <img class="imgCode" @click="getCode" :src="codeIMG" />
            </el-form-item>

            <el-button
              :loading="loading"
              type="primary"
              style="width:100%;margin-bottom:30px;background-color:#2EB1D3;border-color:#2EB1D3;font-size:16px;"
              @click.native.prevent="handleLogin"
            >登录</el-button>
          </div>
        </el-form>
        <img class="right decr" src="~@/assets/images/login/right.png" />
      </div>
    </div>
  </div>
</template>

      <!-- <div class="rlogin-forget">
        <div class="rlogin-regiest"><a href="#/register">注册账号</a></div>
        <div class="rlogin-forgetpassword"><a href="#/forget">忘记密码？</a></div>

      </div>
 -->
    </dir>
  </div>
</template>

<script>
import { hex_sha1 } from '@/utils/sha1'
import { setToken } from '@/utils/auth'
import router, { resetRouter } from '@/router'
import request from '@/utils/request'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value.length==0) {
        callback(new Error("用户名不能为空"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 4) {
        callback(new Error("密码位数不能低于4位"));
      } else {
        callback();
      }
    };

    const validateCode = (rule, value, callback) => {
      if (value.length < 4) {
        callback(new Error("验证码位数不能低于4位"));
      } else {
        callback();
      }
    };
    return {

      userErrorTip: '',
      passwordErrorTip: '',
      codeErrorTip: '',
      passwordType: 'password',
      codeIMG: '',
      loading:false,

      loginForm: {
        username: '',
        password: '',
        uuid: '',
        code: ''
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", validator: validateUsername },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword },
        ],
        code: [{ required: true, trigger: "blur", validator: validateCode }],
      },
    }
  },
  mounted() {
    this.getCode()
  },
  methods: {
    errIconClick: function() {
      this.loginForm.username = ''
    },

    // 获取验证码
    getCode() {
      var that = this
      var t = new Date().getTime()
      request({
        url: `/admin/1.1/codes?t=${t}`, // ie需加时间戳
        method: 'get'
      }).then(response => {
        that.codeIMG = response.data.img64
        that.loginForm.uuid = response.data.uuid

      }).catch(() => {
        that.codeErrorTip = '获取验证码失败'
      })
    },

    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
    },

    handleLogin() {
      sessionStorage.clear()
      var that = this


 this.$refs.loginForm.validate((valid) => {
        if (valid) {
         var loginData = {
        account: this.loginForm.username,
        password: hex_sha1(this.loginForm.password),
        code: this.loginForm.code,
        uuid: this.loginForm.uuid
      }

      request({
        url: `/admin/1.1/sessions`,
        method: 'post',
        data: loginData
      }).then(response => {
        loginData.token = response.data.token
        var token = response.data.token
        setToken(token)
         sessionStorage.setItem(
          'USERNAME',
          JSON.stringify(response.data.userName)
        )
        sessionStorage.setItem(
          'SETUSERNAME',
          JSON.stringify(loginData.account)
        )
        sessionStorage.setItem(
          'AUTHORITY_ARR',
          JSON.stringify(response.data.modules)
        )
        sessionStorage.setItem(
          'LOCAL_UID',
          JSON.stringify(response.data.uid)
        )
        sessionStorage.setItem(
          'PSN_ID',
          JSON.stringify(response.data.pid)
        )
        resetRouter()
        //this.$router.push({ path: '/' })
        window.location.href='/'
      })
        .catch(error => {
          console.log(error.data)

          error.data.errors.forEach((item) => {
            if (item.code === 4200) {
                this.$message('验证码错误');
            } else if (item.code === 4101) {
              this.$message('密码错误');
            } else if (item.code === 4102) {
              this.$message('用户名错误');
            }
          })
          that.getCode()
        })
        } else {
          console.log("error submit!!");
          return false;
        }
      });

    
      
    },

    userNameVerification() {
      if (this.loginForm.username === '') {
        this.userErrorTip = '用户名不能为空'
        return false
      }
      this.userErrorTip = ''
      return true
    },

    passwordVerification() {
      if (
        this.loginForm.password === ''
      ) {
        this.passwordErrorTip = '密码不能为空'
        return false
      }
      this.passwordErrorTip = ''
      return true
    },

    codeVerification() {
      if (this.loginForm.code === '') {
        this.codeErrorTip = '验证码不能为空'
        return false
      }
      /* if (this.loginForm.code.length !== 4) {
        this.codeErrorTip = this.$t("login.codeRule");
        return false;
      }*/
      this.codeErrorTip = ''
      return true
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #ebebeb;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  position: relative;
  overflow: hidden;
  .bg {
    background-image: url("~@/assets/images/login/bg.jpg");
    background-size: cover;
    background-position: center;
    /* filter: blur(5px); */
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
  }
  .el-input {
    display: inline-block;
    height: 45px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    /* border: 1px solid rgba(255, 255, 255, 0.1); */
    background: #174a71;
    border-radius: 1px;
    color: #e4e4e4;
  }
}
</style>

<style lang="scss" scoped>
$bg: #081c3a;
$dark_gray: #fff;
$light_gray: #fff;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  .loginBox {
    position: absolute;
    top: 45%;
    left: 50%;
    transform: translateX(-50%) translateY(-50%);
    .loginForm {
      position: relative;
      padding: 0 120px;
    }
    .en {
      width: 78%;
      margin: auto auto 40px auto;
      display: block;
    }
    .cn {
      width: 90%;
      margin: auto auto 10px auto;
      display: block;
    }
    .decr {
      position: absolute;
      height: 470px;
      top: 0;
      bottom: 0;
      margin: auto;
    }
    .right {
      right: 10px;
    }
    .left {
      left: 10px;
    }
  }
  .login-form {
    background-image: url("~@/assets/images/login/loginBg.png");
    background-size: cover;
    background-position: center;
    width: 555px;
    height: 395px;
    max-width: 100%;
    margin: 0 auto;
    padding: 20px;
    border-radius: 10px;
    overflow: hidden;
    position: relative;
    .loginMain {
      position: absolute;
      top: 50%;
      width: 338px;
      left: 50%;
      transform: translateX(-50%) translateY(-50%);
    }
    .imgCode {
      position: absolute;
      right: 5px;
      top: 50%;
      transform: translateY(-50%);
    }
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 0px 5px 0px 15px;
    color: $light_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    .svg-icon {
      fill: $light_gray;
    }
  }

  .title-container {
    position: relative;

    .title {
      font-size: 22px;
      color: $light_gray;
      margin: 0px auto 20px auto;
      text-align: center;
      /* font-weight: bold; */
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .getCode {
    color: #00e7e7;
    font-size: 13px;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
